--[[
	是一个service，专门处理战绩（钻石战绩，房卡战绩），两者结构相同
	战绩不写入mysql，只在redis里面存储，一个人最多保存20条
]]

local filename = "gameresult.lua"

local skynet = require "skynet"
local cluster = require "skynet.cluster"
require "skynet.manager"

local filelog = require "filelog"
local timetool = require "timetool"
local tabletool = require "tabletool"

local json = require "cjson"
json.encode_sparse_array(true,1,1)

local routerbase = require "routerbase"

local Gameresult = routerbase:new()

local limit = 20

-- 用在这里分割 _ 隔开的 table_uuid
local function split_string(input, delimiter)
	input = tostring(input)
	delimiter = tostring(delimiter)
	if (delimiter=='') then return false end
	local pos,arr = 0, {}
	-- for each divider found
	for st,sp in function() return string.find(input, delimiter, pos, true) end do
		table.insert(arr, string.sub(input, pos, st - 1))
		pos = sp + 1
	end
	table.insert(arr, string.sub(input, pos))
	return arr
end

local function limit_result(rid, resulttype)
	local status, result = skynet.pcall(cluster.call, "datanode", ".router", "select_rk", rid, "rm_" .. resulttype .. "result")
	-- filelog.print(result)
	if not status then return false end
	if result[1] < limit-1 then return true end
	
	local keyid_gameresult = {}
	for i in pairs(result[2]) do
	   table.insert(keyid_gameresult, i)
	end
	table.sort(keyid_gameresult)
	-- print("count : ", #(keyid_gameresult))

	for i=1,#(keyid_gameresult)-limit+1 do
		-- print("sub : " ..  keyid_gameresult[i])
		cluster.send("datanode", ".router", "delete_rk", rid, "rm_" .. resulttype .. "result", keyid_gameresult[i])
	end
end

local function write_result(rid, valuestruct, comment, resulttype)
	local keyid = skynet.call(".nodeuuid", "lua", "getuuid")
	if not comment.table_uuid or not comment.table_tradeid then
		return false
	end
	local tableinfo = split_string(comment.table_uuid, '_')
	local fields = {}
	fields.game_type = string.sub(tableinfo[1], 1, -5)
	fields.table_code= tableinfo[2]
	fields.table_tradeid = tonumber(comment.table_tradeid)
	fields.replay_logname = (comment.replay_logname or '')
    if (resulttype == "diamond" or resulttype == "coin" ) and comment.start_time then
        fields.round_code = string.format("%04d",comment.start_time % 10000)
        local tmp = tostring(fields.table_code) .. fields.round_code
        fields.table_code = tonumber(tmp)
    end
	local other_result = {}
	for k,v in pairs(valuestruct) do
		k = tonumber(k)
		if k == rid then
			fields.self_result = v.value
            fields.self_otherresult = v.othervalue
		else
			table.insert(other_result, {rid=k, rolename=v.rolename, value=v.value, othervalue=v.othervalue})
		end
	end	
	fields.other_result_json = json.encode(other_result)
	local status, result = skynet.pcall(cluster.call, "datanode", ".router", "insert_rk", rid, "rm_" .. resulttype .. "result", keyid, fields)
end

-- 在agent， offline的 game_balance_diamond()调用，最多只保存20条
function Gameresult.CMD.diamond_result(rid, valuestruct, comment)
	-- 先检查已经有多少条战绩，再按时间逆序删除多余的
	limit_result(rid, "diamond")	
	write_result(rid, valuestruct, comment, "diamond")	
end

-- 在agent， offline的 game_balance_fangka()调用，最多只保存20条
function Gameresult.CMD.fangka_result(rid, valuestruct, comment)
	-- 先检查已经有多少条战绩，再按时间逆序删除多余的
	limit_result(rid, "fangka")	
	write_result(rid, valuestruct, comment, "fangka")	
end

-- 在agent， offline的 game_balance_coin()调用，最多只保存20条
function Gameresult.CMD.coin_result(rid, valuestruct, comment)
	-- 先检查已经有多少条战绩，再按时间逆序删除多余的
	limit_result(rid, "coin")	
	write_result(rid, valuestruct, comment, "coin")	
end

skynet.start(function()
	Gameresult:start()
end)
